/*
 *  spa.signup.js
 *  유저에 관련된 작업을 하는 모듈
 */

spa.signup = (function() {
/*-----------------------Module Variable Start----------------------*/
  var configMap = {
    main_html : String()
    + '<div class="form-signup">'
      + '<div class="col-md-8 col-xs-8 col-sm-8">'
        + '<h2 class="form-joinin-heading">회원가입</h2>'
      + '</div>'
      + '<input type="text" class="form-control u_name" placeholder="이름" autofocus>'
      + '<input type="text" class="form-control u_email" placeholder="이메일주소">'
      + '<input type="text" class="form-control u_id" placeholder="아이디">  '
      + '<input type="password" class="form-control" id="id_u_pass_1" name="u_pass_1" placeholder="비밀번호">'
      + '<input type="password" class="form-control" id="id_u_pass_2" name="u_pass_2" placeholder="비밀번호확인">'
      + '<ul class="narrower">'
      + '</ul>'
      + '<button class="btn btn-lg btn-warning btn-block" id="btn-signup">가입하기</button>'
    + '</div>'

  },
  stateMap = {
    $container : null
  },
  jqueryMap = {},
  initModule, setJqueryMap, signUp;
/*-----------------------Module Variable End------------------------*/
/*-----------------------Utility Method Start-----------------------*/
  // /signUp/ Start
  signUp = function() {
    if ( $("#id_u_pass_1").val() !== $("#id_u_pass_2").val() ) {
      alert("비밀번호와 비밀번호 확인이 다릅니다.");
      return false;
    }

    $.ajax({ 
       url : "jsp/newAcc.jsp",
       type: "POST",
       data : { 
        "u_id": $('.u_id').val(),
        "u_pass_1": $('#id_u_pass_1').val(),
        "u_name": $('.u_name').val(),
        "u_email": $('.u_email').val()
       },
       dataType : "json",
       success : function(response) {
        if (response.result === true) {
          alert('성공적으로 가입되었습니다.');
          $(location).attr('href', 'index.html');
        }
        else {
          alert(response.log);
          $(location).attr('href', 'index.html#!modal=signup');
        }
       }
    });
  };
  // /signUp/ End
/*-----------------------Utility Method End-------------------------*/

/*-----------------------DOM Method Start---------------------------*/
  // /setJqueryMap/ Start
  setJqueryMap = function() {
    var $container = stateMap.$container;
    jqueryMap = {
      $container : $container
    };
  };
  // /setJqueryMap/ End
/*-----------------------DOM Method End-----------------------------*/
    
/*-----------------------Event Handler Start------------------------*/
/*-----------------------Event Handler End--------------------------*/

/*-----------------------Public Method Start------------------------*/
  // /initModule/ Start
  initModule = function($container) {
    stateMap.$container = $container;
    $container.html(configMap.main_html);
    setJqueryMap();
    $("#btn-signup").on("click", signUp);
  };
  // /initModule/ End
/*-----------------------Public Method End--------------------------*/
  // Return public function
  return {initModule : initModule};
}());
